﻿/**
 * 倒影类 
 */
package zf.display.image
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.GradientType;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Point;
	
	/**
	 * 超级倒影类 
	 * @author feng.zhang
	 * 
	 */
	public class Reflection extends Sprite
	{
		private var _source:DisplayObject;
		private var _distance:Number;
		private var _colorSize:Number;
		private var _maxAlpha:Number;
		private var _alphaScale:Number;
		private var _leftScale:Number;
		private var _rightScale:Number;
		private var _vScale:Number
		
		/**
		 * 倒影类
		 * @param source 原图
		 * @param distance 距离
		 * @param colorSize 有色区域大小系数
		 * @param maxAlpha 最大透明
		 * @param alphaScale 透明区域大小系数
		 * @param leftScale 左边扩展系数
		 * @param rightScale 右边扩展系数
		 * @param vScale 纵向扩展系数
		 * 
		 */
		public function Reflection(source:DisplayObject,distance:Number=0,colorSize:Number=1,maxAlpha:Number=0.8,alphaScale:Number=0.1,leftScale:Number=0,rightScale:Number=0,vScale:Number=1)
		{
			super();
			
			_source=source;
			_distance=distance;
			_colorSize=colorSize;
			_maxAlpha=maxAlpha;
			_alphaScale=alphaScale;
			//_alphaCentre=alphaCentre;
			_leftScale=leftScale;
			_rightScale=rightScale;
			_vScale=vScale;
			
			createReflection();
		}
		
		private function createReflection():void
		{
			//对源显示对象做上下反转处理
			var bitmapData:BitmapData=new BitmapData(_source.width,_source.height*_colorSize,true,0);
			var matrix:Matrix=new Matrix();
			matrix.d=-1;
			matrix.ty=_source.height;			
			bitmapData.draw(_source,matrix);
			
			//生成一个渐变遮罩
			var w:Number=bitmapData.width;
			var h:Number=bitmapData.height;
			
			matrix=new Matrix();
			matrix.createGradientBox(w,h,0.5*Math.PI);
			
			var shape:Shape=new Shape();
			shape.graphics.beginGradientFill(GradientType.LINEAR,[0,0],[_maxAlpha,0],[0,0xFF*(1-_alphaScale)],matrix);
			shape.graphics.drawRect(0,0,w,h);
			shape.graphics.endFill();
			
			var maskBitmapData:BitmapData=new BitmapData(w,h,true,0);
			maskBitmapData.draw(shape);
			
			//生成最终效果;
			bitmapData.copyPixels(bitmapData,bitmapData.rect,new Point(),maskBitmapData,new Point(),false);
			maskBitmapData.dispose();
			
			var bitmap:Bitmap=new Bitmap();
			bitmap.x=_source.x;
			bitmap.y=_source.y+_source.height+_distance;
			bitmap.bitmapData=bitmapData;
			
			
			if(_leftScale==0 && _rightScale==0 && _vScale==1)
			{
				this.addChild(bitmap);
				return;
			}
			var skew:Skew=new Skew(this,bitmap);
			var x0:Number=bitmap.x;
			var y0:Number=bitmap.y;
			var x1:Number=bitmap.x+bitmap.width;
			var y1:Number=bitmap.y;
			var x2:Number=bitmap.x+bitmap.width*(1+_rightScale);
			var y2:Number=bitmap.y+bitmap.height*_vScale;
			var x3:Number=bitmap.x-bitmap.width*_leftScale;
			var y3:Number=bitmap.y+bitmap.height*_vScale;

			skew.setTransform (x0,y0,x1,y1,x2,y2,x3,y3);
		}
		
		
	}
}